<template>
    <el-card class="account-container">
        <div style="line-height: 100px">

            <el-form :model="form" label-width="120px" ref="setupForm">
                <el-divider content-position="left"><h2>店主信息</h2></el-divider>
                <el-row style="height: 40px"></el-row>
                <el-form-item label="店主姓名" >
                    <el-input disabled v-model="form.sellerName" />
                </el-form-item>
                <el-form-item label="店主电话">
                    <el-input disabled v-model="form.sellerTelephone" />
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="form.idCard"  />
                </el-form-item>
                <el-form-item label="银行卡帐号">
                    <el-input v-model="form.bankCard"  />
                </el-form-item>



                <el-form-item label="身份证图片">
                    <el-upload
                            class="avatar-uploader"
                            action="api/merchant/upload"
                            name="file"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess1"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.idCardImage" :src="form.idCardImage" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-row style="height: 40px"></el-row>
                <el-divider content-position="left"><h2>店铺信息</h2></el-divider>
                <el-row style="height: 40px"></el-row>
                <el-form-item label="商店名称">
                    <el-input v-model="form.shopName" />
                </el-form-item>
                <el-form-item label="咨询电话">
                    <el-input v-model="form.shopTelephone" />
                </el-form-item>
                <el-form-item label="店铺地址">
                    <el-select  v-model="provinceValue" value-key="areaId" placeholder="选择所在省" @change="changeProvince">
                        <el-option
                                v-for="item in arrProvince"
                                :key="item.areaId"
                                :label="item.areaName"
                                :value="item"
                        />
                    </el-select>

                    <el-select  v-model="cityValue" value-key="areaId" placeholder="选择所在市" @change="changeCity">
                        <el-option
                                v-for="item in arrCity"
                                :key="item.areaId"
                                :label="item.areaName"
                                :value="item"
                        />
                    </el-select>

                    <el-select  v-model="districtValue" value-key="areaId" placeholder="选择所在区" @change="changeDistrict">
                        <el-option
                                v-for="item in arrDistrict"
                                :key="item.areaId"
                                :label="item.areaName"
                                :value="item"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input v-model="form.detail" type="textarea" :rows="3" />
                </el-form-item>
                <el-form-item label="店铺介绍">
                    <el-input v-model="form.description" type="textarea" :rows="6" />
                </el-form-item>
                <el-form-item label="店铺图片">
                    <el-upload
                            class="avatar-uploader"
                            action="api/merchant/upload"
                            name="file"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.image" :src="form.image" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-row>
                    <el-col :span="8">
                <el-form-item label="营业执照">
                    <el-upload
                            class="avatar-uploader"
                            action="api/merchant/upload"
                            name="file"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess2"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.certificate" :src="form.certificate" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                    </el-col>
                    <el-col :span="8">
                <el-form-item label="食品安全许可证">
                    <el-upload
                            class="avatar-uploader"
                            action="api/merchant/upload"
                            name="file"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess3"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.license" :src="form.license" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                    </el-col>
                    </el-row>
                <el-form-item>
                    <el-button v-if="hasSubmit" type="primary" @click="submitForm" >提交</el-button>
                    <el-button v-else type="primary" disabled >审核中</el-button>
                </el-form-item>
            </el-form>

        </div>
    </el-card>
</template>

<script  setup>
    import { onMounted,reactive,ref,watch } from 'vue'
    import axios from "axios";
    import {useStore} from 'vuex'
    import {useRouter} from 'vue-router'
    import {ElMessage} from "element-plus";
    import {setup} from "../api/user";
    // do not use same name with ref

    const store=useStore()
    const router=useRouter()
    let arrProvince=ref([]);
    let arrCity=ref([]);
    let arrDistrict=ref([]);
    let provinceValue=ref();
    let cityValue=ref();
    let districtValue=ref();
    let hasSubmit=ref(true)
    let setupForm=ref()
    const form = reactive({
        sellerName:'',
        sellerTelephone:'',
        shopName: '',
        description: '',
        shopTelephone: '',
        image:'',
        provinceName:'',
        provinceId:'',
        cityName:'',
        cityId:'',
        districtName:'',
        districtId:'',
        detail:'',
        idCard:'',
        idCardImage:'',
        certificate:'',
        license:'',
        bankCard:''
    })
    const changeProvince=(val)=>{
        form.provinceName=val.areaName;
        form.provinceId=val.areaId;
        axios.get('/api/test',{
            params:{
                id:val.areaId
            }
        }).then(res=>{
            arrCity.value=res.data.data.data;
        })
    }
    const changeCity=(val)=>{
        form.cityName=val.areaName;
        form.cityId=val.areaId;
        axios.get('/api/test',{
            params:{
                id:val.areaId
            }
        }).then(res=>{
            arrDistrict.value=res.data.data.data;
        })
    }
    const changeDistrict=(val)=>{
        form.districtName=val.areaName;
        form.districtId=val.areaId;
        console.log(form);
    }
    const handleAvatarSuccess=(res)=>{
        form.image=res.data
    }
    const getMessage=()=>{
        const id=store.getters.getId
        axios.get('/api/merchant/get',{
            params:{
                userId:id
            }
        })
            .then(response => {
                // 请求成功处理

                let res=response.data.data;
                console.log(res)
                form.sellerName=res.sellerName
                form.sellerTelephone=res.sellerTelephone
                form.shopName=res.shopName
                form.description=res.description
                form.shopTelephone=res.shopTelephone
                form.image=res.image
                form.detail=res.detail
                form.idCard=res.idCard
                form.idCardImage=res.idCardImage
                form.license=res.license
                form.certificate=res.certificate
                form.bankCard=res.bankCard
                hasSubmit.value=(res.status==0?false:true);
                console.log(hasSubmit);
            })
            .catch(error => {
                // 请求失败处理
                console.error(error);
            });

        axios.get("/api/test",{
            params:{
                id:1
            }
        }).then(res=>{
            arrProvince.value=res.data.data.data;
        })

    }

    onMounted(() => {
        // DOM 元素将在初始渲染后分配给 ref
        console.log("哈哈哈");
        getMessage();
    })

    const ret=()=>{
        router.push({path: "/"})
    }

    const submitForm=()=>{
        axios.post("/api/merchant/setup",form).then(res=>{
            if(res.data.state==0){
                ElMessage.success("添加成功")
                location.reload();
            }else{
                ElMessage.error(res.data.message)
            }
        })
        /*  console.log("submitform启动")
              setupForm.value.validate((valid) => {
                  if (valid) {
                      setup(form).then((res) => {
                          ElMessage.success("提交开店申请成功，等待审核")
                          this.$router.go(0);
                      })
                  } else {
                      ElMessage.error("发生错误，请联系管理")
                      return false;
                  }
              })*/
    }
    const handleAvatarSuccess1=(res)=>{
        form.idCardImage=res.data
    }
    const handleAvatarSuccess2=(res)=>{
        form.certificate=res.data
    }
    const handleAvatarSuccess3=(res)=>{
        form.license=res.data
    }
</script>

<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>